<?php
namespace App\Http\Questionnaire\Controllers;

use Illuminate\Http\Request;
use App\Http\Admin\Controllers\Controller as Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Http\Questionnaire\Controllers\ProblemQController;


class QpGroupController  extends Controller
{
    public function __construct(Request $request)
    {
        $token = getToken($request);
        $uid = Redis::get("code:" . $token);
        $this->uid = $uid;
    }

    public function add_q_type(Request $request){
        $data=$request->only('title','description','type');
        $res = DB::table('q_type')->insertGetId($data);
        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '提交成功',
                'data' => $res
            ];
        }
    }

    public function update_q_type(Request $request,$qType){
        $data=$request->only('title','description','type');
        $res = DB::table('q_type')->where("id",$qType)->update($data);
        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '修改成功',
                'data' => $res
            ];
        }else{
            return [
                'status_code'      => 200,
                'message'     =>  '修改失败 内容未修改',
                'data' => $res
            ];
        }
    }

    public function delete_q_type($qType){
        $res = DB::table('q_type')->where("id",$qType)->update(['status'=>0]);
        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '删除成功',
                'data' => $res
            ];
        }else{
            return [
                'status_code'      => 200,
                'message'     =>  '删除失败 已删除',
                'data' => $res
            ];
        }
    }

    public function select_q_type(){
        $res = DB::table('q_type')->where("status",1)->get();
        return [
            'status_code'      => 200,
            'message'     =>  '查询成功',
            'data' => $res
        ];
    }


    public function add_q_type_group(Request $request){
        $data=$request->only('title','q_type','description');
        $res = DB::table('q_type_group')->insertGetId($data);
        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '新增成功',
                'data' => $res
            ];
        }else{
            return [
                'status_code'      => 200,
                'message'     =>  '新增失败',   
                'data' => $res
            ];
        }
    }
//
    public function update_q_type_group(Request $request,$qTGroup){
        $data=$request->only('title','q_type','description');
        $res = DB::table('q_type_group')->where('id',$qTGroup)->update($data);
        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '修改成功',
                'data' => $res
            ];
        }else{
            return [
                'status_code'      => 200,
                'message'     =>  '修改失败 内容未修改',
                'data' => $res
            ];
        }
    }

    public function delete_q_type_group($qTGroup){
        $res = DB::table('q_type_group')->where('id',$qTGroup)->update(['status'=>0]);
        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '删除成功',
                'data' => $res
            ];
        }else{
            return [
                'status_code'      => 200,
                'message'     =>  '删除失败 任务已删除',
                'data' => $res
            ];
        }
    }

    public function select_q_type_group($qType){
        $res = DB::table('q_type_group')->where([['status',1],['q_type',$qType]])->orderBy("id","desc")->get()->toArray();
        foreach($res as $k=>$v){
            $res[$k]['child']=DB::table('q_tg_qp')->where("q_type_group",$v['id'])->get();
        }
        return [
            'status_code'      => 200,
            'message'     =>  '查询成功',
            'data' => $res
        ];
    }

    public function add_q_type_group_qp(Request $request){
        $data = $request->only('q_type','q_type_group','qpid','taskTitle','groupTitle');
        $res = DB::table('q_tg_qp')->insertGetId($data);
        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '新增成功',
                'data' => $res
            ];
        }else{
            return [
                'status_code'      => 200,
                'message'     =>  '新增失败',
                'data' => $res
            ];
        }
    }

    public function delete_q_type_group_qp(Request $request){
        $data = $request->only('q_type','q_type_group','qpid');
        $res = DB::table('q_tg_qp')->where([
            ['q_type',$data['q_type']],
            ['q_type_group',$data['q_type_group']],
            ['qpid',$data['qpid']]
        ])->delete();
        if($res){
            return [
                'status_code'      => 200,
                'message'     =>  '删除成功',
                'data' => $res
            ];
        }else{
            return [
                'status_code'      => 200,
                'message'     =>  '删除失败 任务已删除',
                'data' => $res
            ];
        }
    }

    /**
     * @param int $qType
     * @return array data
     */
    public function get_q_type_questionaire(Request $request,$qType){
        $qs = DB::table("q_questionnaire")->where('type',$qType)->get()->toArray();
        $pq=new ProblemQController($request);
        foreach($qs as $k=>$v){
            $qs[$k]['child']=$pq->select_tree($request,$v['id'])['data'];
        } 

        return [
            'status_code'      => 200,
            'message'     =>  '查询成功',
            'data' => $qs
        ];
    }


    public function demo_case(Request $request){
        $res = DB::table("c_case_data")->where("cid",151)->first();
        $re="/(.|\n)*?原告.*与/m";
        preg_match($re,$res['content'],$data2);
        $newContent = preg_replace("/(户籍[*]*?|住).*(.|。)/","住**。",$data2[0]);

        $newContent = preg_replace("/".$data2[0]."/",$newContent,$res['content']);
        $newContent = preg_replace('/^1[34578]\d{9}$/ims','**',$newContent);
        $newContent = preg_replace('/^(0\d{2,3})?(\d{7,8})$/ims','**',$newContent);
        $newContent = preg_replace('/^[1-9]\d{5}[1-9]\d{3}((0\d)|bai(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/i','**',$newContent);
        // $newContent = preg_replace('/([0-9]{0,15})/','**',$newContent);
        
        return $newContent;
        // var_dump($data2);
    }

}